<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_top" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="message_error_top_text" class="noty_text">Hubo un problema durante la ejecución.</span></div>
</div>

<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text"><?php echo $success_message;?></span></div>
    </div>
<?php endif; ?>


<script type="text/javascript">
	function calcular_total_guia(){
		$('#subtotalgeneral').val(0);
		
		$('#table_details tr').each(function (i) {
			var total_subtotalcost = $(this).find("input[name*=total_subtotalcost]").val();
			
			if(total_subtotalcost != undefined){
				total_subtotalcost = total_subtotalcost.replace(",","");
				var total_guia = $('#subtotalgeneral').val();
				
				if(total_guia == '' || total_guia == undefined) total_guia = 0;

				total_guia = parseFloat(total_guia) + parseFloat(total_subtotalcost);
				
				$('#subtotalgeneral').val(parseFloat(total_guia).toFixed(2));
			}
			
			
		});
	}
    $(document).ready(function() {
		
        $('#noty_success_1355937644582').click(function() {
            $('#noty_success_1355937644582').fadeOut('fast');
        });
    })
</script>
<div class="grid_container">
    <form id="formAddPurchase" action="<?php echo site_url("shop/submit");?>" method="post" class="form_container">
		<input type="hidden" name="shop_id" value="<?php if(isset($shop)) echo $shop->id_shop; ?>">
        <div class="grid_12">
            <div class="widget_wrap">
                <div class="widget_top">
                    <span class="h_icon magnifying_glass"></span>
                    <h6>
						<?php echo $form_title; ?> 
                    </h6>
                </div>

                <div class="widget_content" style="margin-bottom: 30px">
                    <ul>
                        <li>
                            <span class="label_head"></span>
                            <div class="form_grid_12">
                                <div class="form_input">
                                    <div class=" form_grid_4 alpha">
										<span class="label_head">N° Documento</span>
										<input type="text" name="numero" id="numero" class="noEmpty" value="<?php if(isset($shop)) echo $shop->document_code; ?>" tabindex="2">
                                    </div>
									<div><span></span></div>
									
									<div id="fecha">
									<div class=" form_grid_4 alpha">
										<span class="label_head">Fecha  </span>
                                        <input type="text" name="date"  id="numero" value="<?php if(isset($shop)) {echo $shop->date;} else { echo date('d/m/Y');} ?>" tabindex="3">
									</div>
									</div> 
									<span class="clear"></span>
								</div>
							</div>
						</li>
						<li>
                            <div class="form_grid_12">
                                <div class="form_input">
                                       <div class="form_grid_5 alpha">

										<span class="label_head" for="type_document">Razón Social </span>
                                        <?php
                                        $options = array();
                                        $options[0] = "";
                                        foreach ($company as $td) {
                                            $options[$td->id_company] = $td->name;
                                        }
                                        $extra = 'id="company" tabindex="3" style="width:340px" class="chzn-select full" data-placeholder="Elegir..."';
										$selected_company = '';
										if(isset($shop)) $selected_company = $shop->id_supplier;
										echo form_dropdown('supplier', $options, $selected_company, $extra);
                                        ?>
										</div>
                                <span class="clear"></span> 
                                </div>
                            </div>
                     
                        </li>
                        <li>
                            <div class="form_grid_12">
                                <div class="form_input">
									<div class="form_grid_8 alpha">
                                        <span class="label_head">Observaciones</span>
                                        <input type="text" name="observations" id="observations" tabindex="5" value="<?php if(isset($shop)) echo $shop->observations;?>">
                                    </div>
                                    <span class="clear"></span>
                                </div>
                            </div>
						</li>
                    </ul>
			
					<script>
						$(document).ready(function() {
							$('#addProduct').click(function(e) {
								$.ajax({
									'url': url + '/product_selection_modal/',
									'type': 'POST',
									'data': {
										'code': '',
										'description': '',
										'needProductsStock': '0'
									},
									'success': function(res) {
										$('#add-product-model').html(res);
									}
								});
								$('#add-product-model').modal();
								return false;
							});

						})
					</script>

					<div class="form_grid_2 " style="margin-bottom: 10px; float: right">
						<button id="addProduct" class="btn_small btn_blue"><span>Agregar Producto</span></button>
					</div>
                    
                    <table id="table_details" class="wtbl_list">
                        <thead>
                            <tr class="tr_1">
                                <th width="10%">Código</th>
                                <th width="30%">Descripción</th>
                                <th width="10%">Cajas</th>
                                <th width="10%">Empaques</th>
                                <th width="10%">Unidades</th>
                                <th width="10%">Costo Empaque</th>
                                <th width="10%">Total</th>
                                <th width="10%"></th>
                            </tr>

                        </thead>

                        <tbody id="contentProductList" class="contentProductList">
<?php
	if(isset($shop_details)){
		foreach ($shop_details as $detail) {
?>
			<tr >
				<td align="left">
					<input type="hidden" name="product_id_detalle[]" value="<?php echo $detail->products_id; ?>">
					<?php echo $detail->code; ?>
				</td>
				<td align="left">
					<?php echo $detail->description; ?>
				</td>
				<td align="center">
					<input type="hidden" name="total_cajas[]" value="<?php echo $detail->box; ?>">
					<?php echo $detail->box; ?>
				</td>
				<td align="center">
					<input type="hidden" name="total_paquetes[]" value="<?php echo $detail->pack; ?>">
					<?php echo $detail->pack; ?></td>
				<td align="center">
					<input type="hidden" name="total_unidades[]" value="<?php echo $detail->unit; ?>">
					<?php echo $detail->unit; ?>
				</td>
				<td align="right">
					<input type="hidden" name="total_cost[]" value="<?php echo $detail->Val_Unit; ?>">
					<?php echo "US$ ".number_format((float)$detail->Val_Unit, 2); ?></td>
				<td align="right">
					<input type="hidden" name="total_subtotalcost[]" value="<?php echo $detail->amount; ?>">
					<?php echo "US$ ".number_format((float)$detail->amount, 2); ?></td>
				<td align="center">
					<a href="#" id="remove-this-row-<?php echo $detail->products_id; ?>" class="action-icons c-Delete">Delete</a>
				
<?php			
				foreach ($warehouse_distribution as $distribution) {
					if($distribution->product_id == $detail->products_id){
?>
						<input type="hidden" name="product_id_almacenes[]" value="<?php echo $distribution->product_id; ?>">
						<input type="hidden" name="id_almacen[]" value="<?php echo $distribution->id_warehouse; ?>">
						<input type="hidden" name="cajas_de_almacen[]" value="<?php echo $distribution->box; ?>">
						<input type="hidden" name="paquetes_de_almacen[]" value="<?php echo $distribution->pack; ?>">
						<input type="hidden" name="unidades_de_almacen[]" value="<?php echo $distribution->unit; ?>">
<?php				}
				} ?>
				</td>
			</tr>
			
			<script type="text/javascript">
				$('#remove-this-row-<?php echo $detail->products_id; ?>').click(function(event) {
					$(this).closest("tr").remove();
					calcular_total_guia();
					return false; 
				});
			</script>
<?php
		}
	}
?>                            
						</tbody>
                    </table>
					    <input type="text" name="subtotalgeneral" id="subtotalgeneral" placeholder="0.00" value="<?php if(isset($shop)) echo $shop->amount;?>" readonly="true" style=" text-align:center; width: 100px; margin-right: 58px; float: right; background: #D0D0D0; font-family: arial; font-size: 15px; font-weight: bold;">
						<input type="text" value="TOTAL US$" readonly="true" style=" text-align:right; width: 100px; float: right; background: #D0D0D0; font-family: arial; font-size: 15px; font-weight: bold;">
                 </div>
            </div>
            <br />

                <script type="text/javascript">
				
                    $(document).ready(function() {
						
                        function checkCode() {
                            $.ajax({
                                'url': url + '/shop/search_code_unique',
                                'type': 'POST',
                                'data': {'numero': $('input[name=numero]').val()},
                                'success': function(res) {
                                    if (res == 1) {
										$('div[id=message_error_top]').css('display', 'block');
										$('span[id=message_error_top_text]').html('El código del documento ya fue ingresado anteriormente al sistema.');
                                        $('#numero').css('background', '#ff3333');
                                    } else if (res == 0) {
                                        $('#numero').css('background', '#ffffff');
                                        $('#formAddPurchase').submit();
                                    }
                                }
                            });
                        }
					
                        $('#numero').focus(function() {
                            $('#numero').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })

                        function formtrue() {
                            $('formAddPurchase').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddPurchase').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_guia').click(function() {
                            var codigo = $('#numero').val();
							var fecha = $('input[name=date]').val();
							var filas_en_detalle = $('#table_details tr').length;
							
							if (codigo.length < 1 || codigo == ''){
								$('div[id=message_error_top]').css('display', 'block');
								$('span[id=message_error_top_text]').html('Ingrese el número de documento.');
							} else if (fecha.length < 1 || fecha == ''){
								$('div[id=message_error_top]').css('display', 'block');
								$('span[id=message_error_top_text]').html('Ingrese la fecha.');
							} else if(filas_en_detalle < 2){
								$('div[id=message_error_top]').css('display', 'block');
								$('span[id=message_error_top_text]').html('Debe agregar algún producto');
							} else {
								<?php if(isset($shop)){?>
										$('#formAddPurchase').submit();
								<?php }else{ ?>
										checkCode();
								<?php } ?>
								
                            } 
                        });
                    })

                </script>

                <button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button>

                
        </div>
    </form>
    <span class="clear"></span>

    <div id="add-product-model">
    </div>
</div>
			<script type="text/javascript">
				//calcular_total_guia();
			</script>
<table id="listProductBlank" class="clear">
    <tr id="rowListProductBlank" class="tr_even">
        <td class="elementList codeProduct"></td>
        <td class="elementList descriptionProduct"></td>
        <td class="elementList quantityBoxesProduct"></td>
        <td class="elementList quantityPackagesProduct"></td>
        <td class="elementList quantityUnitsProduct"></td>
        <td class="elementList priceProduct"></td>
        <td class="elementList inputProduct"><a href="#" class="action-icons c-Delete delProductList" original-title="Borrar">Delete</a></td>
    </tr>
</table>